<?php 
class SockReporter implements Reporter{
    private static $instance=null;

    public function __construct(){}

    public static function getInstance(){
        if(self::$instance === null){
            self::$instance = new SockReporter();
        }

        return self::$instance;
    }

    public function post($url){
        $parts=parse_url($url);

        $fp = fsockopen($parts['host'],
              isset($parts['port'])?$parts['port']:80,
              $errno, $errstr, 30);

        if (!$fp) {
          return false;
        } else {
          $out = "POST ".$parts['path']." HTTP/1.1\r\n";
          $out.= "Host: ".$parts['host']."\r\n";
          $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
          $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
          $out.= "Connection: Close\r\n\r\n";
          if (isset($parts['query'])) $out.= $parts['query'];

          $response = fwrite($fp, $out);
          fclose($fp);
          return $response;
        }
    }

    public function sendMessage($content, $post_url){
        $export = var_export($content, true);
        return $this->post($post_url . "?content=" . $content);
    }
}

class CurlReporter implements Reporter{
    private $handler=null;
    private $timeout=null; // in seconds
    private $url=null; // set default url here
    private $options=null;
    private $fieldsCount=null;
    private $fieldsString=null;
    public static $instance=null;

    const DEFAULT_TIMEOUT = 5; // in seconds
    //const DEFAULT_RETURNTRANSFER = true;

    public function __construct(){
        $this->handler = curl_init();
    }

    public static function getInstance(){
        if(self::$instance === null){
            self::$instance = new CurlReporter();
        }

        return self::$instance;
    }

    public function post($content){
        $this->fieldsString = "content=".$content;
        $this->fieldsCount = 1;
        $this->setOptionsArray();
        $this->setCurlOptions();

        return $this->exec();
    }

    public function sendMessage($content, $post_url){
        $this->setUrl($post_url);
        $result = $this->post($content);
        return $result;
    }

    public function setUrl($url){
        $this->url = $url;
    }

    public function setTimeout($seconds){
        $this->timeout = $seconds;
    }

    private function setOptionsArray(){
        $this->options = array(CURLOPT_URL => $this->url,
                               CURLOPT_CONNECTTIMEOUT => ($this->timeout==null) ? self::DEFAULT_TIMEOUT : $this->timeout,
                               CURLOPT_POST => $this->fieldsCount,
                               CURLOPT_POSTFIELDS => $this->fieldsString);
    }

    private function setCurlOptions(){
        curl_setopt_array($this->handler, $this->options);
    }

    private function exec(){
        // perform some testing here; the script hangs when a url isn't properly set
        
        $contents = curl_exec($this->handler);
        return $contents;
    }
}

?>